package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.CompanyUser;
import com.banksoft.cps.bean.common.Page;
import com.banksoft.cps.bean.common.ServiceException;
import com.banksoft.cps.bean.query.CompanyUserQuery;
import com.banksoft.cps.constants.ErrorConstant;
import com.banksoft.cps.constants.WebConstant;
import com.banksoft.cps.dao.CompanyUserDao;
import com.banksoft.cps.enums.UserType;
import com.banksoft.cps.service.CompanyUserService;
import com.banksoft.cps.util.CacheUtil;
import com.banksoft.cps.util.StrGenerator;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rongzhao.zhu on 2017/12/20.
 */
@Service
public class CompanyUserServiceImpl implements CompanyUserService {

    @Autowired
    CompanyUserDao companyUserDao;

    @Override
    public CompanyUser login(String account, String password) throws ServiceException {
        if(StringUtils.isBlank(account)){
            throw new ServiceException(ErrorConstant.ec_param_null,ErrorConstant.em_user_account_null);
        }
        if(StringUtils.isBlank(password)){
            throw new ServiceException(ErrorConstant.ec_param_null,ErrorConstant.em_user_password_null);
        }
        CompanyUser companyUser = companyUserDao.findByAccount(account);
        if(companyUser == null ){
            throw new ServiceException(ErrorConstant.ec_result_null,ErrorConstant.em_user_null);
        }
        if(!companyUser.getPassword().equals(password)){
            throw new ServiceException(ErrorConstant.ec_param_error,ErrorConstant.em_password_error);
        }
        String token =  token(companyUser);
        Map<String,String> cookieMap = new HashMap<>();
        cookieMap.put(WebConstant.cookieToken,token);
        cookieMap.put(WebConstant.cookieFake, StrGenerator.generateRandomStr(32));
        cookieMap.put(WebConstant.cookieName, StrGenerator.EncoderByMd5(companyUser.getName()));
        companyUser.setCookieMap(cookieMap);
        companyUser.setType(UserType.company.name());
        CacheUtil.cache(token,companyUser);
        return companyUser;
    }
    @Override
    public void logout(String token) {
        CacheUtil.remove(token);
    }
    @Override
    public List<CompanyUser> list(CompanyUserQuery query) {
        List<CompanyUser>   companyUsers =  companyUserDao.list(query);
        return companyUsers;
    }

    @Override
    public long count(CompanyUserQuery query) {
        return companyUserDao.count(query);
    }

    @Override
    public CompanyUser findById(String id) {
        if(StringUtils.isNotBlank(id)){
            CompanyUserQuery query =new CompanyUserQuery();
            query.setId(id);
            List<CompanyUser> companyUsers = list(query);
            if(companyUsers !=null && companyUsers.size()>0){
                return companyUsers.get(0);
            }
        }
        return null;
    }

    @Override
    public CompanyUser findByCompanyId(String companyId) {
        if(StringUtils.isNotBlank(companyId)){
            CompanyUserQuery query =new CompanyUserQuery();
            query.setCompanyId(companyId);
            List<CompanyUser> companyUsers = list(query);
            if(companyUsers !=null && companyUsers.size()>0){
                return companyUsers.get(0);
            }
        }
        return null;
    }

    @Override
    public void save(CompanyUser companyUser) throws ServiceException{
        CompanyUserQuery query =new CompanyUserQuery();
        query.setAccount(companyUser.getAccount());
        List<CompanyUser> userInDb = companyUserDao.list(query);
        boolean exist = userInDb.stream().anyMatch(u->!u.getId().equals(companyUser.getId()));
        if(exist){
            throw new ServiceException(ErrorConstant.ec_account_exist,ErrorConstant.em_account_exist);
        }
        if(StringUtils.isNotBlank(companyUser.getId())){
            companyUserDao.update(companyUser);
        }else {
            companyUser.setId(UUIDFactory.random());
            companyUser.setCreateTime(System.currentTimeMillis());
            companyUserDao.insert(companyUser);
        }
    }

    @Override
    public void updatePassword(String id, String oldPassword, String newPassword) throws ServiceException {
        CompanyUser bean = companyUserDao.findById(id);
        if(!bean.getPassword().equals(oldPassword)){
            throw new ServiceException(ErrorConstant.ec_param_error,ErrorConstant.em_password_error);
        }
        bean.setPassword(newPassword);
        companyUserDao.update(bean);
    }
}
